package main

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"google.golang.org/grpc/credentials/insecure"
	"project05/src/proto"
)

var userClient proto.UserServiceClient
var conn *grpc.ClientConn

func Init() {
	var err error
	conn, err = grpc.Dial("127.0.0.1:8081", grpc.WithTransportCredentials(insecure.NewCredentials()))
	if err != nil {
		fmt.Println("err", err)
		return
	}

	userClient = proto.NewUserServiceClient(conn)
}

func GetUserList() {
	rsp, err := userClient.GetUserList(context.Background(), &proto.PageInfo{
		Pn:    1,
		PSize: 2,
	})

	if err != nil {
		panic(err)
	}
	userInfo, err := userClient.CreateUser(context.Background(), &proto.CreateUserInfo{
		UserName: "张三",
		Password: "admin123",
		Identify: "12345678910",
		Phone:    "13907959868",
	})
	fmt.Println(userInfo)

	for _, user := range rsp.Date {
		fmt.Println(user)
		checkRep, err := userClient.CheckPassword(context.Background(), &proto.CheckPasswordInfo{
			Password:        "admin123",
			EncryptPassword: user.Password,
		})
		if err != nil {
			panic(err)
		}
		fmt.Println("check", checkRep)
	}
}

func main() {
	Init()
	GetUserList()
}
